import axios from 'axios'
import api from "./server-api";

var _url = api.ip
// var _url = 'http://47.107.159.133:80'

const goodsList = (callback) => {
  // const url = _url + '/goods/list'
  const url = _url + '/goods/page'
  axios.get(url).then(num => {
    callback && callback(num.data)
  })
}

//搜索
const searchApi = (input, callback) => {
  const url = _url + '/goods/page?goodsName=' + input;
  axios.get(url).then(num => {
    callback && callback(num.data);
  })
}

//分页
const pagingList = (val,input,callback) => {
  var inputUrl;
  if (input == "" || input == null){
    inputUrl =  '/goods/page?limit=5&pages='+val
  }else {
    inputUrl = '/goods/page?limit=5&pages='+val+'&goodsName=' + input
  }
  const url = _url  + inputUrl
  axios.get(url).then(num => {
    callback && callback(num.data)
  })
}

const goodsDelApi = (goodsId,callback) => {
  const url = _url + '/goods/?goodsId=' + goodsId
  axios.delete(url).then(num => {
    callback && callback(num)
  })
}


const dialogFormVisiblePutGoodsApi = (listPutGoods, callback) => {
  var _listPut = new URLSearchParams();
  _listPut.append("goodsId",listPutGoods.goodsId);
  _listPut.append("goodsName",listPutGoods.goodsName);
  _listPut.append("goodsImg",listPutGoods.goodsImg);
  _listPut.append("goodsPrice",listPutGoods.goodsPrice);
  _listPut.append("goodsDescribe",listPutGoods.goodsDescribe);
  _listPut.append("monthlySales",listPutGoods.monthlySales);
  _listPut.append("totalSales",listPutGoods.totalSales);
  _listPut.append("goodsType",listPutGoods.goodsType);
  _listPut.append("typeId",listPutGoods.goodsType);
  _listPut.append("categoryId",listPutGoods.categoryId);
  const url = _url + '/goods/?' + _listPut;
  axios.put(url).then(num => {
    callback && callback(num.data);
  })
}

const dialogFormVisiblePostGoodsApi = (listPostGoods, callback) => {
  var _listPut = new URLSearchParams();
  //添加自动追加id
  // _listPut.append("goodsId",null);
  _listPut.append("goodsName",listPostGoods.goodsName);
  _listPut.append("goodsImg",listPostGoods.goodsImg);
  _listPut.append("goodsPrice",listPostGoods.goodsPrice);
  _listPut.append("goodsDescribe",listPostGoods.goodsDescribe);
  _listPut.append("monthlySales",listPostGoods.monthlySales);
  _listPut.append("totalSales",listPostGoods.totalSales);
  _listPut.append("categoryId",listPostGoods.categoryId);
  _listPut.append("goodsType",listPostGoods.goodsType);
/*  _listPut.append("goodsType",listPostGoods.goodsType);
  _listPut.append("categoryId",listPostGoods.categoryId);*/

  /*先设置搞定的，后面添加选择器*/
  //已添加，已解决
  // _listPut.append("goodsType",1);
  // _listPut.append("categoryId",1);

  const url = _url + '/goods/?' + _listPut;
  axios.post(url).then(num => {
    callback && callback(num.data);
  })
}

const goodsStateztApi = (ztId,ztIndex,callback) => {
  var zt;
  if(ztId == 1){
    zt = 'xiaJia';
  }else if(ztId == 0){
    zt = 'shangJia';
  }else {
    alert("服务出错请刷新")
  }
  const url = _url + '/goods/'+zt+'?goodsId=' + ztIndex;

  axios.put(url).then(num => {
    callback && callback(num)
  })
}

//商品详细
const detailedApi = (detailedId,callback) => {
  const url = _url + '/goodsCharacter/list?goodsId=' + detailedId
  axios.get(url).then(num => {
    callback && callback(num.data)
  })
}

//添加商品时，查看所有的大类
const categoryList = (callback) => {
  const url = _url + '/category/list'
  axios.get(url).then(num => {
    callback && callback(num.data)
  })
}

//点击大类触发对应的小类
const typesCategoryId = (id,callback) => {
  const url = _url + '/types/?categoryId=' + id;
  axios.get(url).then(num => {
    console.log(num.data);
    callback && callback(num.data);
  })
}

export {
  goodsList, // 自动刷新,查看所有
  goodsDelApi, // 根据id删除商品
  dialogFormVisiblePutGoodsApi, // 根据id修改商品
  dialogFormVisiblePostGoodsApi, //添加商品
  goodsStateztApi, // 是否上架商品
  detailedApi, //商品详细
  categoryList, //添加商品时，查看所有的大类
  typesCategoryId, //点击大类触发对应的小类
  pagingList,  //分页
  searchApi,  //搜索

}
